﻿using CoverShooter;
using Nanolink;
using NanolinkDemo;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnlineManager : MonoBehaviour {
    public GameObject SelfPlayerObj;
    public GameObject OtherPlayerObj;
    public GameObject SelfPlayer;
    public List<GameObject> OtherPlayers;
    public GameObject CameraObj;
    public GameObject[] SpawnPoint;
    public List<GameObject> Roles;
    public Image Scope;

    // Use this for initialization
    void Start ()
    {
        OtherPlayers = new List<GameObject>();
    }

    public void CreateSelf(int index)
    {
        print("sssssssssss");
        SelfPlayer = GameObject.Instantiate(SelfPlayerObj);
        SelfPlayer.transform.position = SpawnPoint[index].transform.position;
        SelfPlayer.name = "Player" + index;
        CameraObj.GetComponent<ThirdPersonCamera>().Target = SelfPlayer.GetComponent<CharacterMotor>();
        SelfPlayer.GetComponent<ThirdPersonController>().Scope = Scope;
        CreatePlayerEvent();
    }


    public void CreatePlayerEvent()
    {
        Hashtable values = new Hashtable();
        values.Add("name", "Create");
        values.Add("Pos", Vector3.zero);
        print("CreatePlayerEvent=" + NanoClient.getString("status"));
        MyClient.send(MySerialize.toBytes(values));
    }

    public void CreateOther(int index)
    {
        GameObject OtherPlayer = GameObject.Instantiate(OtherPlayerObj);
        OtherPlayer.transform.position = SpawnPoint[index].transform.position;
        OtherPlayer.name = "Player" + index;
        OtherPlayers.Add(OtherPlayer);
    }

    /*public void InitGame(int ClientIndex)
    {
        print("ClientIndex="+ ClientIndex);
        Roles = new List<GameObject>();
        int playerIndex = ClientIndex;
        for (int i = 0; i < playerIndex; i++)
        {
            if (i < SpawnPoint.Length)
            {
                GameObject Role = Instantiate(LeadObj);
                Role.transform.position = SpawnPoint[i].transform.position;
                Role.name = "Player" + i;
                Role.GetComponent<ThirdPersonInput>().isLead = false;
                Roles.Add(Role);
            }
        }
        Roles[playerIndex-1].GetComponent<ThirdPersonInput>().isLead = true;
        CameraObj.GetComponent<ThirdPersonCamera>().Target = Roles[playerIndex - 1].GetComponent<CharacterMotor>();
    }*/

    // Update is called once per frame
    void Update () {
		
	}
}
